######################
#  Replication code for 'Mediating the Electoral Connection', forthcoming in the JOP
#  John Henderson and John Brooks
#  12/7/2015    
######################    

# figureVII-imputed-plots.R
#  :: produces the plots for the leave-one-year-out IV analysis imputing missing in Figure VII in the online appendix 

rm(list=ls())
setwd('~/Dropbox/rainReplication')

load('leaveout/figureVII_imputed-4_3.Rdata') 

years=rownames(coefs_clst[[1]])
ci=ses=betas=list()

# cluster day
betas[[1]]=(coefs_clst[[1]][,1])
ses[[1]]=(coefs_clst[[1]][,2])
betas[[2]]=(coefs_clst[[2]][,1])
ses[[2]]=(coefs_clst[[2]][,2])

ci[[1]]=cbind(betas[[1]]+ses[[1]]*1.645,betas[[1]]-ses[[1]]*1.645)
ci[[2]]=cbind(betas[[2]]+ses[[2]]*1.645,betas[[2]]-ses[[2]]*1.645)

pdf('leaveout/leave_one_out_day_cluster_impute.pdf')                          
par(cex.axis=.9)
plot(betas[[1]]~years,main='',xlab='Left Out Year',ylab='IV Estimate',ylim=c(-7,2),col='grey20',pch=19,axes=F    )
for(i in 1:nrow(ci[[1]])){
	lines(x=c(years[i],years[i]),y=c(ci[[1]][i,1],ci[[1]][i,2]),lty=2)
}
axis(2)
abline(h=0)
lines(lowess(f=.6,betas[[1]]~years),col='grey20',lty=2)
axis(1,at=c(years),labels=years)
dev.off()  



# cluster weekend

pdf('leaveout/leave_one_out_weekend_cluster_impute.pdf')                          
par(cex.axis=.9)
plot(betas[[2]]~years,main='',xlab='Left Out Year',ylab='IV Estimate',ylim=c(-7,2),col='grey20',pch=19,axes=F    )
for(i in 1:nrow(ci[[2]])){
	lines(x=c(years[i],years[i]),y=c(ci[[2]][i,1],ci[[2]][i,2]),lty=2)
}
axis(2)
abline(h=0)
lines(lowess(f=.6,betas[[2]]~years),col='grey20',lty=2)
axis(1,at=c(years),labels=years)
dev.off()  

# end